<?php
/**
 *
 */
class Sx_useropen_mod extends CI_Model {
	var $sx_return = FALSE;
	var $sx_key;
	var $sx_sessionId;
	var $sx_return_arr = FALSE;
	var $r_return = array();
	var $tab_user = "sx_users";

	function __construct() {
		parent::__construct();
	}

	/**
	 *
	 * user_con 判断用户是否为登录状态
	 * $mobikye 手机key
	 * $id session_id
	 * $rarr 是否返回用户名称和登录时间
	 */
	function user_con($mobikye = '', $id = '', $rarr = FALSE) {
		$this -> sx_key = $mobikye;
		$this -> sx_sessionId = $id;
		$this -> sx_return_arr = $rarr;
		//如果客户端传入session_id不存在或者为空，返回为假
		(empty($this -> sx_key) || empty($this -> sx_sessionId)) ? ($this -> sx_return = FALSE) : ($this -> sx_return = TRUE);
		//判断用户名是否存在

		if ($this -> sx_return) {
			session_id($id);
			@session_start();
			(isset($_SESSION['hck_user']) && !empty($_SESSION['hck_user'])) ? ($this -> sx_return = TRUE) : ($this -> sx_return = FALSE);

		}

		//$newkey =$this->fengchaoencode->url_base64_decode($this -> fengchaoencode -> sxtimecode($this -> sx_key, $operation = 'DECODE', $key = '', $expiry = 1800));
		$newkey = $this -> fengchaoencode -> url_base64_decode($this -> sx_key);
		//判断key是否失效
		empty($newkey) ? ($this -> sx_return = FALSE) : ($this -> sx_return = TRUE);
		//比对Key和数据库保存是否一致
		if ($this -> sx_return) {

			$newkeyarr = explode("||", $newkey);
			$row = array();
			$this -> db -> where('user_login', $newkeyarr[0]);
			$Q = $this -> db -> get($this -> tab_user);
			if ($Q -> num_rows() > 0) {
				$row = $Q -> row_array();
				$dbk = $this -> fengchaoencode -> url_base64_decode($this -> sx_key);
				//$dbk = $this->fengchaoencode->url_base64_decode($this -> fengchaoencode -> sxtimecode($row['user_mobi_key'], $operation = 'DECODE', $key = '', $expiry = 1800));
				if ($dbk != $newkey) {
					$this -> sx_return = FALSE;
				} else {
					session_id($id);
					@session_start();
					$r_return = array();
					//$r['username'] =$newkeyarr[0] ;
					if (!isset($_SESSION['hck_user']) || empty($_SESSION['hck_user'])) {
						$this -> sx_return = FALSE;
					} else {
						$this -> r_return['userid'] = $row['ID'];
						$this -> r_return['username'] = $_SESSION['hck_user'];
						$this -> r_return['nicename'] = $row['user_nicename'];
						$this -> r_return['userlogintime'] = $newkeyarr['2'];
						$this -> sx_return = TRUE;
						$this -> sx_return_arr = TRUE;
					}

				}

			}
			$Q -> free_result();

		}
		if ($this -> sx_return_arr == TRUE && $this -> sx_return == TRUE) {
			return $this -> r_return;
		} else {
			return $this -> sx_return;
		}

	}

	// ----------------------------------------------------------------
	/*
	 * 查询用户信息
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @param	$num  查询条数 	string
	 * @return	array 没有数据，返回假
	 */
	// ----------------------------------------------------------------
	function userall($uid = "", $start = "", $num = "") {
		$data = array();
		$this->db->select("ID,user_email,user_login,user_nicename,user_city,user_img,user_url,user_intr");
		if (!empty($uid)) {
			$this -> db -> where('ID', $uid);
		}
		$this -> db -> order_by('ID', "desc");
		empty($start) ? $start = 0 : $start;
		if (empty($num)) {
			$Q = $this -> db -> get($this -> tab_user);
		} else {
			$Q = $this -> db -> get($this -> tab_user, $num, $start);
		}
		if ($Q -> num_rows() > 0) {
			foreach ($Q->result_array() as $row) {
				$data[] = $row;
				
			}
			return $data;
		} else {
			return FALSE;
		}
		$this -> db -> free_result();
	}

	// ----------------------------------------------------------------
	/*
	 * 修改用户资料
	 * @access	public
	 * @param	$uid  用户ID 	string
	 * @param	$arr   	array()
	 * @return	array 没有数据，返回假
	 */
	// ----------------------------------------------------------------

	function userEdit($arr, $uid) {
		$r = FALSE;
		$this -> db -> where('ID', "$uid");
		$this -> db -> update("$this->tab_user", $arr);
		$this -> db -> affected_rows() > 0 ? $r = TRUE : $r = FALSE;
		return $r;
	}



}
?>